<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {

        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $redis = new \Redis();
        $redis->connect('127.0.0.1',6379);
        // $redis->auth(111);
        $fk_count  = $redis->get('count'); // 总流量0 $redis->get('count'); // 总流量
        $fk_httpIp = $redis->sMembers('httpIp'); //访客
        $httptime  = $redis->sMembers('iptime');
        $browsetime = $redis->sMembers('browsetime');

        $yesterday = 0;
        $httpweek = 0;
        $httpmonth = 0;
        $httpyear = 0;
        $today = date('Ymd',time());
        $week = date("Ymd",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));
        $weej = date("Ymd",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));
        $monthk = date('Ym01');
        $monthend = date('Ymt');
        $beginThismonth=date("Y",time())."1"."1";
        $endThismonth=date("Y",time())."12"."31";
        $browseyesterday = 0;
        $browseweek = 0;
        $browsemonth = 0;
        $browseyear = 0;
        foreach ($browsetime as $key)
        {
            $ztime = date('Ymd',$key);
            if ($ztime = $today)
            {
                $browseyesterday += 1;
            }elseif($ztime >= $week && $ztime<=$weej){
                $browseweek += 1;
            }elseif ($ztime >= $monthk && $ztime<=$monthend){
                $browsemonth += 1;
            }elseif($ztime >= $beginThismonth && $ztime<=$endThismonth){
                $browseyear += 1;
            }
        }


        foreach ($httptime as $key)
        {
            $ztime = date('Ymd',$key);
            if ($ztime = $today)
            {
                $yesterday += 1;
            }elseif($ztime >= $week && $ztime<=$weej){
                $httpweek += 1;
            }elseif ($ztime >= $monthk && $ztime<=$monthend){
                $httpmonth += 1;
            }elseif($ztime >= $beginThismonth && $ztime<=$endThismonth){
                $httpyear += 1;
            }
        }


        $httpIpcount = count($fk_httpIp);
        $column = [];
        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');
        $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
            ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $userlist = array_fill_keys($column, 0);
        foreach ($joinlist as $k => $v) {
            $userlist[$v['join_date']] = $v['nums'];
        }

        $addonList = get_addon_list();
        $totalworkingaddon = 0;


        foreach ($addonList as $index => $item) {
            if ($item['state']) {
                $totalworkingaddon += 1;
            }
        }
        $countOrder              = Db::name('order')->where('order_status',0)->count();
        $behalfcountOrder         = Db::name('order')->where('order_status',1)->count();
        $salescountOrder         = Db::name('order_sale')->where('status',0)->count();
        $evaluation = Db::name('order')->where('order_status',3)->count();

        $countcomment       = Db::name('order')->where('order_status',3)->count();
        $countusershop      = Db::name('user_shop')->where('status',1)->count();
        $countuseragent     = Db::name('user_agent')->where('status',1)->where('is_oneagent',0)->count();

        $time = time();
        //交易
        $orderpricedb = Db::name('order');
        // 销售件数
        $goodsnum = $orderpricedb->where('pay_status',1)->sum('goods_num');

        $orderCount = $orderpricedb->count();
        $orderCountmonth = $orderpricedb->whereTime('create_time','month')->count();
        $orderCountyear = $orderpricedb->whereTime('create_time','year')->count();
        $orderpayprice = $orderpricedb->where('order_status','>',0)->where('pay_way',1)->sum('goods_pay_price');
        $userprice = Db::name('user_order')->where('status',1)->sum('price');
        $tradingprice = bcadd($orderpayprice, $userprice, 2);

        $orderzf = $orderpricedb->where('order_status','>',0)->where('pay_way',1)->sum('goods_pay_price');
        $orderzfyesterday = $orderpricedb->whereTime('create_time','yesterday')->where('order_status','>',0)->where('pay_way',1)->sum('goods_pay_price');
        $orderzfweek = $orderpricedb->whereTime('create_time','week')->where('order_status','>',0)->where('pay_way',1)->sum('goods_pay_price');
        $orderzfmonth = $orderpricedb->whereTime('create_time','month')->where('order_status','>',0)->where('pay_way',1)->sum('goods_pay_price');
        $orderzfyear = $orderpricedb->whereTime('create_time','year')->where('order_status','>',0)->where('pay_way',1)->sum('goods_pay_price');


        // 订单数量
        $orderNum = $orderpricedb->count();
        $orderNumyesterday = $orderpricedb->where('pay_status','>',0)->whereTime('create_time','yesterday')->count();
        $orderNumweek  = $orderpricedb->where('pay_status','>',0)->whereTime('create_time','week')->count();
        $orderNummonth = $orderpricedb->where('pay_status','>',0)->whereTime('create_time','month')->count();
        $orderNumyear  = $orderpricedb->where('pay_status','>',0)->whereTime('create_time','year')->count();


        // 用户下单人数
        if (empty($orderusercount) || empty($orderNum))
        {
            $paymentprice = 0;
        }else{
            bcdiv($httpIpcount,$orderNum,2);
        }


        $ordergoods = $orderpricedb->where('pay_status','>',0)->sum('goods_num');
        $ordergoodsyesterday = $orderpricedb->where('pay_status','>',0)->whereTime('create_time','yesterday')->sum('goods_num');
        $ordergoodsweek  = $orderpricedb->where('pay_status','>',0)->whereTime('create_time','week')->sum('goods_num');
        $ordergoodsmonth = $orderpricedb->where('pay_status','>',0)->whereTime('create_time','month')->sum('goods_num');
        $ordergoodsyear  = $orderpricedb->where('pay_status','>',0)->whereTime('create_time','year')->sum('goods_num');


        $goods_pay_price = $orderpricedb->where('pay_status','>',0)->sum('goods_pay_price');
        $order_xs_count  = $orderpricedb->where('pay_status','>',0)->count();
        $order_xs_countyesterday = $orderpricedb->where('pay_status','>',0)->whereTime('create_time','yesterday')->count();
        $order_xs_counweek   = $orderpricedb->where('pay_status','>',0)->whereTime('create_time','week')->count();
        $order_xs_countmonth = $orderpricedb->where('pay_status','>',0)->whereTime('create_time','month')->count();
        $order_xs_countyear  = $orderpricedb->where('pay_status','>',0)->whereTime('create_time','year')->count();

        $kdjprice = empty($goods_pay_price) || empty($order_xs_count) ? 0 :  bcdiv($goods_pay_price, $order_xs_count,2);
        $kdjpriceyesterday = empty($goods_pay_price) || empty($order_xs_countyesterday) ? 0 :  bcdiv($goods_pay_price, $order_xs_countyesterday,2);
        $kdjpriceweek = empty($goods_pay_price) || empty($order_xs_counweek) ? 0 :  bcdiv($goods_pay_price, $order_xs_counweek,2);
        $kdjpricemonth = empty($goods_pay_price) || empty($order_xs_countmonth) ? 0 :  bcdiv($goods_pay_price, $order_xs_countmonth,2);
        $kdjpriceyear = empty($goods_pay_price) || empty($order_xs_countyear) ? 0 :  bcdiv($goods_pay_price, $order_xs_countyear,2);

        $tkprice = $orderpricedb->where('pay_status',2)->sum('goods_pay_price');
        $tkpriceyesterday  = $orderpricedb->where('pay_status',2)->whereTime('create_time','yesterday')->sum('goods_pay_price');
        $tkpriceweek  = $orderpricedb->where('pay_status',2)->whereTime('create_time','week')->sum('goods_pay_price');
        $tkpricemonth  = $orderpricedb->where('pay_status',2)->whereTime('create_time','month')->sum('goods_pay_price');
        $tkpriceyear  = $orderpricedb->where('pay_status',2)->whereTime('create_time','year')->sum('goods_pay_price');

        $shop_price = $orderpricedb->where('pay_status','>',0)->where('user_status',1)->sum('goods_pay_price');
        $shop_pricetodayyesterday  = $orderpricedb->where('pay_status','>',0)->where('user_status',1)->whereTime('create_time','yesterday')->sum('goods_pay_price');
        $shop_pricetodayweek  = $orderpricedb->where('pay_status','>',0)->where('user_status',1)->whereTime('create_time','week')->sum('goods_pay_price');
        $shop_pricetodaymonth  = $orderpricedb->where('pay_status','>',0)->where('user_status',1)->whereTime('create_time','month')->sum('goods_pay_price');
        $shop_pricetodayyear  = $orderpricedb->where('pay_status','>',0)->where('user_status',1)->whereTime('create_time','year')->sum('goods_pay_price');

        $agent_price = $orderpricedb->where('pay_status','>',0)->where('user_status','>',1)->sum('goods_pay_price');
        $agent_priceyesterday  = $orderpricedb->where('pay_status','>',0)->where('user_status','>',1)->whereTime('create_time','yesterday')->sum('goods_pay_price');
        $agent_priceweek   = $orderpricedb->where('pay_status','>',0)->where('user_status','>',1)->whereTime('create_time','week')->sum('goods_pay_price');
        $agent_pricemonth  = $orderpricedb->where('pay_status','>',0)->where('user_status','>',1)->whereTime('create_time','month')->sum('goods_pay_price');
        $agent_priceyear   = $orderpricedb->where('pay_status','>',0)->where('user_status','>',1)->whereTime('create_time','year')->sum('goods_pay_price');

        $userorder = $orderpricedb->group('uid')->count();
        $userorderyesterday = $orderpricedb->group('uid')->whereTime('create_time','yesterday')->count();
        $userorderweek = $orderpricedb->group('uid')->whereTime('create_time','week')->count();
        $userordermonth = $orderpricedb->group('uid')->whereTime('create_time','month')->count();
        $userorderyear = $orderpricedb->group('uid')->whereTime('create_time','year')->count();

        $userordergm = $orderpricedb->group('uid')->where('pay_status','>',0)->count();
        $userordergmyesterday = $orderpricedb->group('uid')->where('pay_status','>',0)->whereTime('create_time','yesterday')->count();
        $userordergmweek = $orderpricedb->group('uid')->where('pay_status','>',0)->whereTime('create_time','week')->count();
        $userordergmonth = $orderpricedb->group('uid')->where('pay_status','>',0)->whereTime('create_time','month')->count();
        $userordergmyear = $orderpricedb->group('uid')->where('pay_status','>',0)->whereTime('create_time','year')->count();

        $usersc = Db::name('goods_relation')->group('uid')->where('type','collect')->count();
        $userscyesterday = Db::name('goods_relation')->group('uid')->whereTime('create_time','yesterday')->where('type','collect')->count();
        $userscweek = Db::name('goods_relation')->group('uid')->whereTime('create_time','week')->where('type','collect')->count();
        $userscmonth = Db::name('goods_relation')->group('uid')->whereTime('create_time','month')->where('type','collect')->count();
        $userscyear = Db::name('goods_relation')->group('uid')->whereTime('create_time','year')->where('type','collect')->count();

        $usergidsc = Db::name('goods_relation')->group('gid')->where('type','collect')->count();
        $usergidscyesterday = Db::name('goods_relation')->group('gid')->whereTime('create_time','yesterday')->where('type','collect')->count();
        $usergidscweek = Db::name('goods_relation')->group('gid')->whereTime('create_time','week')->where('type','collect')->count();
        $usergidscmonth = Db::name('goods_relation')->group('gid')->whereTime('create_time','month')->where('type','collect')->count();
        $usergidscyear = Db::name('goods_relation')->group('gid')->whereTime('create_time','year')->where('type','collect')->count();
        $usercar = Db::name('user')->count();
        $usercartoday = Db::name('user_car')->whereTime('create_time','today')->group('uid')->count();

        $usershop = Db::name('user_shop')->count();
        $usershoptoday = Db::name('user_shop')->whereTime('create_time','today')->count();

        $useragent = Db::name('user_agent')->where('is_oneagent',0)->count();
        $useragenttoday = Db::name('user_agent')->whereTime('create_time','today')->where('is_oneagent',0)->count();

        $useragentq = Db::name('user_agent')->where('is_oneagent',1)->count();
        $useragentqtoday = Db::name('user_agent')->whereTime('create_time','today')->where('is_oneagent',1)->count();
        $goodscount = Db::name('goods')->count();
        $brandcount = Db::name('brand')->count();
        $ordergoodsnum = $orderpricedb->where('pay_status',1)->sum('goods_num');

        $this->view->assign([
            // 商品数量
            'goodscount' => $goodscount,
            // 销售数量
            'ordergoodsnum' => $ordergoodsnum,
            // 品牌数量
            'brandcount' => $brandcount,
            // 销售总数
            'goodsnum' => $goodsnum,
            // 区域代理商数量
            'useragentq' => $useragentq,
            'useragentqtoday' => $useragentqtoday,
            // 代理商数量
            'useragent' => $useragent,
            'useragenttoday' => $useragenttoday,
            // 安装店数量
            'usershop' => $usershop,
            'usershoptoday' => $usershoptoday,
            // 车主数量
            'usercar' => $usercar,
            'usercartoday' => $usercartoday,
            // 收藏商品数
            'usergidsc' => $usergidsc,
            'usergidscyesterday' => $usergidscyesterday,
            'usergidscweek' => $usergidscweek,
            'usergidscmonth' => $usergidscmonth,
            'usergidscyear' => $usergidscyear,
            // 收藏人数
            'usersc' => $usersc,
            'userscyesterday' => $userscyesterday,
            'userscweek' => $userscweek,
            'userscmonth' => $userscmonth,
            'userscyear' => $userscyear,

            // 累计交易额
            'tradingprice' => $tradingprice,

            // 转化率
            'conversion'  => $paymentprice,
            // 客单价
            'unitprice'  => $httpIpcount == 0  || $goodsnum == 0 ? 0 : bcdiv($goods_pay_price, $goodsnum,2),
            // 支付金额
            'orderzf' => $orderzf,
            'orderzfyesterday' => $orderzfyesterday,
            'orderzfweek' => $orderzfweek,
            'orderzfmonth' => $orderzfmonth,
            'orderzfyear' => $orderzfyear,
            // 支付转换
            'paymentprice' => $httpIpcount == 0  || $goodsnum == 0 ? 0 : bcdiv($goods_pay_price, $goodsnum,2),
            'orderNumyesterday'  => $httpIpcount == 0  || $orderNumyesterday == 0 ? 0 : bcdiv($goods_pay_price, $orderNumyesterday,2),
            'orderNumweek'  => $httpIpcount == 0  || $orderNumweek == 0 ? 0 : bcdiv($goods_pay_price, $orderNumweek,2),
            'orderNummonth'  => $httpIpcount == 0  || $orderNummonth == 0 ? 0 : bcdiv($goods_pay_price, $orderNummonth,2),
            'orderNumyear'  => $httpIpcount == 0  || $orderNumyear == 0 ? 0 : bcdiv($goods_pay_price, $orderNumyear,2),

            // 支付订单数
            'ordergoods' => $ordergoods,
            'ordergoodsyesterday' => $ordergoodsyesterday,
            'ordergoodsweek' => $ordergoodsweek,
            'ordergoodsmonth' => $ordergoodsmonth,
            'ordergoodsyear' => $ordergoodsyear,

            // 客单价
            'kdjprice'  => $kdjprice,
            'kdjpriceyesterday'  => $kdjpriceyesterday,
            'kdjpriceweek'  => $kdjpriceweek,
            'kdjpricemonth'  => $kdjpricemonth,
            'kdjpriceyear'  => $kdjpriceyear,
            // 退款金额
            'tkprice'  => $tkprice,
            'tkpriceyesterday'  => $tkpriceyesterday,
            'tkpriceweek'  => $tkpriceweek,
            'tkpricemonth'  => $tkpricemonth,
            'tkpriceyear'  => $tkpriceyear,

            // 安装店
            'shop_price'    => $shop_price,
            'shop_pricetodayyesterday'    => $shop_pricetodayyesterday,
            'shop_pricetodayweek'     => $shop_pricetodayweek,
            'shop_pricetodaymonth'    => $shop_pricetodaymonth,
            'shop_pricetodayyear'     => $shop_pricetodayyear,
            // 代理商
            'agent_price'    => $agent_price,
            'agent_priceyesterday'    => $agent_priceyesterday,
            'agent_priceweek'     => $agent_priceweek,
            'agent_pricemonth'    => $agent_pricemonth,
            'agent_priceyear'     => $agent_priceyear,

            // 访客
            'httpIpcount' => $httpIpcount,
            'yesterday' => $yesterday,
            'httpweek' => $httpweek,
            'httpmonth' => $httpmonth,
            'httpyear' => $httpyear,

            // 总流量
            'fk_count'  => $fk_count == '' ? 0 : $fk_count,
            'browseyesterday'  => $browseyesterday,
            'browseweek'  => $browseweek,
            'browsemonth'  => $browsemonth,
            'browseyear'  => $browseyear,

            // 加购人数
            'userorder' => $userorder,
            'userorderyesterday' => $userorderyesterday,
            'userorderweek' => $userorderweek,
            'userordermonth' => $userordermonth,
            'userorderyear' => $userorderyear,
            // 购买人数
            'userordergm' => $userordergm,
            'userordergmyesterday' => $userordergmyesterday,
            'userordergmweek' => $userordergmweek,
            'userordergmonth' => $userordergmonth,
            'userordergmyear' => $userordergmyear,


            'orderCount'    => $orderCount,
            'orderCountmonth'  => $orderCountmonth,
            'orderCountyear'  => $orderCountyear,

            'totaluser'        => User::count(),
            'behalfcountOrder'       => $behalfcountOrder,
            'salescountOrder'       => $salescountOrder,
            'countOrder'         => $countOrder,
            'evaluation'         => $evaluation,

            'countcomment'         => $countcomment,
            'countusershop'         => $countusershop,
            'countuseragent'         => $countuseragent,


        ]);

        $this->assignconfig('column', array_keys($userlist));
        $this->assignconfig('userdata', array_values($userlist));

        return $this->view->fetch();
    }

}

